﻿<template>
	<full-screen-dialog :title="titleMap[mode]" v-model="visible" :width="1300" destroy-on-close
		:close-on-click-modal="false" :before-close="handleBeforeClose">

    <!-- <el-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1300"
        destroy-on-close
        :close-on-click-modal="false"
        :before-close="handleBeforeClose"
    > -->
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="促销活动标题" prop="activityName">
                        <el-input v-model="entityObj.activityName" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="活动时间" prop="activityDates">
                        <el-date-picker
                            style="width: 300px"
                            v-model="entityObj.activityDates"
                            type="datetimerange"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            format="YYYY-MM-DD HH:mm:ss"
                            range-separator="-"
                            unlink-panels="true"
                            start-placeholder="开始时间"
                            end-placeholder="结束时间"
                            :default-time="defaultTime"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="适配单位" prop="purchaseUnityId">
                        <el-select
                            placeholder="---请选择状态---"
                            v-model="entityObj.purchaseUnityId"
                            style="width: 100%"
                        >
                            <el-option label="---请选择---" value />
                            <el-option
                                v-for="item in optionsPurchaseUnity"
                                :key="item.purchaseUnityId"
                                :label="item.purchaseUnityName"
                                :value="item.purchaseUnityId"
                            />
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="创建人" prop="crName">
                        <el-input v-model="entityObj.crName" />
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="创建时间" prop="crDate">
                        <el-date-picker
                            v-model="entityObj.crDate"
                            :default-time="defaultTime"
                            type="datetime"
                            value-format="YYYY-MM-DD"
                            format="YYYY-MM-DD"
                            placeholder="请输入创建时间"
                            style="width: 100%"
                        />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="备注">
                        <el-input v-model="entityObj.remarks" type="textarea" :rows="2" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label-width="120px" label="促销物料信息" prop="supplierActivityItems">
                        <el-button size="small" @click="addMaterial()">新增物料</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px">
                        <el-table
                            :data="entityObj.supplierActivityItems"
                            border
                            style="width: 100%"
                            show-summary
                        >
                            <el-table-column label="物料编码"  min-width="100">
                                <template #default="scope">{{scope.row.materialCode}}</template>
                            </el-table-column>
                            <el-table-column label="物料名称" min-width="130">
                                <template #default="scope">{{scope.row.materialName}}</template>
                            </el-table-column>
                            <el-table-column label="规格"  min-width="120">
                                <template #default="scope">{{scope.row.materialSpecs}}</template>
                            </el-table-column>
                            <el-table-column label="物料类别"  min-width="120">
                                <template #default="scope">{{scope.row.materialTypeName}}</template>
                            </el-table-column>
                            <el-table-column label="单位"  min-width="80">
                                <template #default="scope">{{scope.row.unitName}}</template>
                            </el-table-column>
                            <el-table-column label="原价"  min-width="80">
                                <template #default="scope">{{scope.row.price}}</template>
                            </el-table-column>
                            <el-table-column label="促销类型"  min-width="140">
                                <template #default="scope">
                                    <el-select
                                        placeholder="---请选择状态---"
                                        v-model="scope.row.activityType"
                                        style="width: 100%"
                                    >
                                        <el-option label="---请选择---" value />
                                        <el-option
                                            v-for="item in optionsActivityType"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        />
                                    </el-select>
                                </template>
                            </el-table-column>
                            <el-table-column label="促销数量"  min-width="140">
                                <template #default="scope">
                                    <el-input-number
                                        v-model="scope.row.activityQty"
                                        style="width: 100%"
                                        :step="1"
                                        controls-position="right"
                                    ></el-input-number>
                                </template>
                            </el-table-column>
                            <el-table-column label="促销价"  min-width="140">
                                <template #default="scope">
                                    <el-input-number
                                        v-model="scope.row.activityPrice"
                                        style="width: 100%"
                                        :step="0.01"
                                        controls-position="right"
                                    ></el-input-number>
                                </template>
                            </el-table-column>
                            <el-table-column label="到期时间"  min-width="160" >
                                <template #default="scope">
									<!--不能填写到期时间 -->
                                   <!-- <el-date-picker
                                        v-if="scope.row.activityType==0"
                                        v-model="scope.row.expireDate"
                                        :default-time="defaultTime"
                                        type="datetime"
                                        value-format="YYYY-MM-DD"
                                        format="YYYY-MM-DD"
                                        placeholder="请输入创建时间"
                                        style="width: 100%"
                                    />
                                    <span v-else>-</span> -->
									<el-date-picker
									    v-model="scope.row.expireDate"
									    :default-time="defaultTime"
									    type="datetime"
									    value-format="YYYY-MM-DD"
									    format="YYYY-MM-DD"
									    placeholder="请输入创建时间"
									    style="width: 100%"
									/>
                                </template>
                            </el-table-column>
                            <el-table-column label="适配门店"  min-width="180">
                                <template #default="scope">
                                    <sc-store-select
                                        style="width: 160px"
                                        ref="orgselect"
                                        :props="storeProps"
                                        :selectKeyValue="'storeId'"
                                        v-model="scope.row.stores"
                                        @fatherMethod="orgSelectProcess"
                                    ></sc-store-select>
                                </template>
                            </el-table-column>
                            <el-table-column label="操作"  min-width="80">
                                <template #default="scope">
                                    <el-button
                                        size="small"
                                        @click="deleteRow(scope.$index, scope.row)"
                                    >移除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
            <el-button @click="handleBeforeClose">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="submit()"
            >保 存</el-button>
        </template>

        <template>
            <el-dialog
                v-model="materialState"
                title="选择商品"
                :width="1300"
                :center="true"
                append-to-body
                style="height: 480px"
            >
                <el-row>
                    <sc-material-choice ref="scMaterialChoice" :queryType="'purchase-protocol/item-pages'"></sc-material-choice>
                </el-row>
                <template #footer>
                    <el-button @click="materialState = false">取 消</el-button>
                    <el-button type="primary" :loading="isSaveing" @click="setMaterialChoice">确定</el-button>
                </template>
            </el-dialog>
        </template>
    </full-screen-dialog>
</template>



<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                supplierActivityItems: [],
            },
            materialState: false,
            storeState: false,
            queryList: {},
            visible: false,
            isSaveing: false,
            needSaveFlag: false,
            //验证规则
            rules: {
                activityName: [
                    { required: true, type: "string", message: "请填写" },
                ],

                purchaseUnityId: [
                    { required: true, type: "number", message: "请填写" },
                ],
                suppliers: [
                    { required: true, type: "object", message: "请填写" },
                ],
                crName: [{ required: true, type: "string", message: "请填写" }],
                crDate: [{ required: true, type: "string", message: "请填写" }],
                activityDates: [
                    {
                        required: true,
                        type: "array",
                        message: "请填写",
                        trigger: "change",
                    },
                ],
                supplierActivityItems: [
                    {
                        required: true,
                        type: "array",
                        message: "请填写",
                        trigger: "change",
                    },
                ],
            },
            storeProps: {
                label: "storeName",
                value: "storeId",
            },
            warehouseProps: {
                label: "warehouseName",
                value: "warehouseId",
            },
            materialProps: {
                label: "materialCode",
                value: "materialId",
                items: "materialItems",
            },
            optionsActivityType: [],
            optionsSourceType: [],
            optionsInStockType: [],
            optionsPurchaseUnity:[],
            itemsData: [],
            defaultTime:[
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 2, 1, 23, 59, 59),
            ]
        };
    },
    computed: {},
    methods: {
        //显示=
        async open(mode = "Add") {
            this.mode = mode;
            this.visible = true;

            return this;
        },
        handleBeforeClose() {
            this.$confirm("信息未保存,确定要关闭吗？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.visible = false;
                })
                .catch(() => {
                    // 用户点击了取消按钮，不执行关闭
                });
        },
        //表单提交方法
        submit() {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                this.mode == "Add"
                                    ? "supplier-activity/create"
                                    : "supplier-activity/update",
                                { ...this.entityObj}
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                            this.isSaveing = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        this.isSaveing = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData(id) {
            if (id && id > 0) {
                try {
                    var returnValue = await this.$API.auth.GetProcessApi.get(
                        "supplier-activity/detail/" + id,
                        {}
                    );
                    if (returnValue.status == 200) {
                        this.entityObj = returnValue.response;
                        this.entityObj.suppliers = {
                            supplierId: this.entityObj.supplierId,
                            supplierName: this.entityObj.supplierName,
                        };
                        this.entityObj.warehouses = {
                            warehouseId: this.entityObj.warehouseId,
                            warehouseName: this.entityObj.warehouseName,
                        };
                    } else {
                        this.$message.error("操作失败:" + returnValue.msg);
                    }
                } catch (error) {
                    this.loading = false;
                    return false;
                }
            }
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["ActivityType"] }
                );
                if (returnValue.status == 200) {
                    this.optionsActivityType = returnValue.response.find(
                        (p) => p.code == "ActivityType"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }

            try {
                //采购单位列表
                var returnValue = await this.$API.auth.GetProcessApi.get(
                    "purchase-unity/list"
                );
                if (returnValue.status == 200) {
                    this.optionsPurchaseUnity = returnValue.response;
                    this.entityObj.purchaseUnityId =
                        this.optionsPurchaseUnity[0].purchaseUnityId;
                    this.entityObj.purchaseUnityName =
                        this.optionsPurchaseUnity[0].purchaseUnityName;
                    this.entityObj.purchaseRealName =
                        this.optionsPurchaseUnity[0].realName;
                    this.entityObj.purchaseMobile =
                        this.optionsPurchaseUnity[0].mobile;
                    this.entityObj.purchaseAddress =
                        this.optionsPurchaseUnity[0].address;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        addMaterial() {
            this.materialState = true;
        },
        deleteRow(index) {
            this.entityObj.supplierActivityItems.splice(index, 1);
        },
        setMaterialChoice() {
            let materials = this.$refs.scMaterialChoice.selectionRows;
            materials.map((p) => {
                debugger;
                this.entityObj.supplierActivityItems.push({
                    materialId: p.materialId,
                    materialCode: p.materialCode,
                    materialName: p.materialName,
                    materialSpecs: p.materialSpecs,
                    materialTypeId: p.materialTypeId,
                    materialTypeName: p.materialTypeName,
                    unitName: p.unitName,
                    price: p.price,
                    materialItemId: p.materialItemId,
                });
            });
            this.materialState = false;
        },
        purchaseUnityChange(value) {
            this.entityObj.purchaseRealName = this.optionsPurchaseUnity.find(
                (p) => p.purchaseUnityId == value
            ).realName;
            this.entityObj.purchaseMobile = this.optionsPurchaseUnity.find(
                (p) => p.purchaseUnityId == value
            ).mobile;
        },
    },
    async mounted() {
        this.entityObj.inStockDate = tool.formatDate(new Date(), 1);
        this.entityObj.crName = tool.data.get("USER_INFO").Name;
        this.entityObj.crDate = tool.formatDate(new Date());
        await this.initOptions();
    },
};
</script>

<style></style>
